package cn.edu.cqut.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.edu.cqut.entity.Chance;
import cn.edu.cqut.entity.Plan;
import cn.edu.cqut.service.IChanceService;
import cn.edu.cqut.util.PageResult;
import cn.edu.cqut.util.ResultChanceWithPlans;

/**
 * <p>	
 *  前端控制器
 * </p>
 *
 * @author HQYJ
 * @since 2020-06-24
 */
@RestController
/*
 * 如果是Controller返回的就是jsp页面，加rest之后返回json文件
 */

@RequestMapping("/chance")
@CrossOrigin  //允许跨域请求 
public class ChanceController {

	@Autowired  
	private IChanceService chanceService;
	
	@RequestMapping("/getChanceList")
	public PageResult<Chance> getChanceList(
			@RequestParam(defaultValue = "1") Integer page, //RequestParam设置默认请求
			@RequestParam(defaultValue = "10") Integer limit,
			Chance chance){
		QueryWrapper<Chance> queryWapper = new QueryWrapper<>();
		if(chance.getChName()!= null) {
			// 模糊查询条件： 第一个是表里面的一个字段名
			queryWapper.like("ch_name", chance.getChName());
		}
		if(chance.getChRole() != null ) {
			queryWapper.like("ch_role", chance.getChRole());
		}
		Page<Chance> page3 = new Page<Chance>(page,limit);
		Page<Chance> page2 = chanceService.page(page3,queryWapper);
		PageResult<Chance> pr = new PageResult<>();
		pr.setCode(0);
		pr.setCount(page2.getTotal()); //总数
		pr.setMsg("");
		pr.setData(page2.getRecords());		
		return pr;
		// page分页对象  getRecords得到list列表
	}
	//修改客户信息
		@RequestMapping("/updateChance")
		public PageResult<Chance> updateChance(Chance chance){
			chanceService.updateById(chance);
			System.out.println("updateChance");
			PageResult<Chance> pr = new PageResult<>();
			pr.setMsg("修改销售机会成功！");
			pr.setCode(0);	
			return pr;
		}
		//新增客户信息
		@RequestMapping("/addChance")
		public PageResult<Chance> addCustomer(Chance chance){
			chanceService.save(chance);
			PageResult<Chance> pr = new PageResult<>();
			pr.setMsg("新增销售机会成功！");
			pr.setCode(0);	
			return pr;
		}
		//删除客户信息
		@RequestMapping("/delChance")  //添加前端映射
		public PageResult<Chance> delChance(Integer[] ids){
			chanceService.removeByIds(Arrays.asList(ids));  //数组转list
			PageResult<Chance> pr = new PageResult<>();
			pr.setMsg("删除销售机会成功！");
			pr.setCode(0);
			return pr;
		}
		@RequestMapping("/getChanceWithPlan")
		public PageResult<ResultChanceWithPlans> getChanceWithPlan(
				@RequestParam(defaultValue = "1") Integer page, //@RequestParam给参数设置默认值
				@RequestParam(defaultValue = "10") Integer limit,
				Chance chance
				){
			QueryWrapper<Chance> queryWapper = new QueryWrapper<>();
			if(chance.getChName()!=null) {
				//模糊查询条件，第一个参数是表的字段名，不是实体的属性名
				queryWapper.like("ch_name", chance.getChName());
			}
			Page<Chance> page3 = new Page<Chance>(page,limit);
			Page<Chance> page2 = chanceService.page(page3, queryWapper);
			List<Chance> chances = chanceService.getChanceWithPlan();
			
			List<ResultChanceWithPlans> chances1 = new ArrayList<>();
			
			for(Chance cc:chances) {
				if(cc.getChState()==1) {
					
				
				if(cc.getPlans().size()!=0) {
					List<Plan> plans = cc.getPlans();
					for(Plan pl:plans) {
						ResultChanceWithPlans rcwp = new ResultChanceWithPlans();
						rcwp.setPlContent(pl.getPlContent());
						rcwp.setPlDate(pl.getPlDate());
						rcwp.setPlNo(pl.getPlNo());
						rcwp.setPlState(pl.getPlState());
						rcwp.setChNo(cc.getChNo());
						rcwp.setChName(cc.getChName());
						chances1.add(rcwp);
					}
					
				}else {
					ResultChanceWithPlans rcwp = new ResultChanceWithPlans();
					rcwp.setChNo(cc.getChNo());
					rcwp.setChName(cc.getChName());
					rcwp.setPlContent("");
					chances1.add(rcwp);
				}
				}
			}
			
			PageResult<ResultChanceWithPlans> pr = new PageResult<>();
			pr.setCode(0);
			pr.setCount(page2.getTotal());
			pr.setMsg("");
			pr.setData(chances1);
			return pr;

}
		
}

